package com.tanhua.server.controller;


import com.tanhua.model.mongo.UserComment;
import com.tanhua.model.vo.PageResult;
import com.tanhua.server.service.SmallVideosService;
import org.bson.types.ObjectId;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;

import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.ws.rs.Path;
import java.io.IOException;
import java.util.Map;

@RestController
@RequestMapping("/smallVideos")
public class SmallVideoController {

    @Autowired
    private SmallVideosService videosService;


    /**
     * 发布视频
     *  接口路径：POST
     *  请求参数：
     *      videoThumbnail： 封面图
     *      videoFile：视频文件
     * @param videoThumbnail
     * @param videoFile
     * @return
     */
    @PostMapping
    public ResponseEntity saveVideos(MultipartFile videoThumbnail,MultipartFile videoFile) throws IOException {
        videosService.saveVideos(videoThumbnail,videoFile);
        return ResponseEntity.ok(null);
    }

    /**
     * 视频列表查询
     */
    @GetMapping
    public ResponseEntity queryVideoList(@RequestParam(defaultValue = "1")  Integer page,
                                         @RequestParam(defaultValue = "10") Integer pagesize) {
        PageResult result = videosService.queryVideoList(page, pagesize);
        return ResponseEntity.ok(result);
    }

    /**
     * 视频用户关注
     */
    @PostMapping("/{uid}/userFocus")
    private ResponseEntity userFocus(@PathVariable("uid") Long followUserId){
        videosService.userFocus(followUserId);
        return ResponseEntity.ok(null);
    }

    /**
     * 视频用户取消关注
     */
    @PostMapping("/{uid}/userUnFocus")
    private ResponseEntity userUnFocus(@PathVariable("uid") Long followUserId){
        videosService.userUnFocus(followUserId);
        return ResponseEntity.ok(null);
    }

    /**
     *  视频用户点赞
     */
    @PostMapping("/{id}/like")
    private ResponseEntity like(@PathVariable("id") ObjectId id){
        videosService.userLike(id);
        return ResponseEntity.ok(null);
    }

    /**
     * 视频用户取消点赞
     */
    @PostMapping("/{id}/dislike")
    private ResponseEntity dislike(@PathVariable("id") ObjectId id){
        videosService.userDisLike(id);
        return ResponseEntity.ok(null);
    }

    /**
     * 查看视频评论列表
     */
    @GetMapping("/{id}/comments")
    public ResponseEntity comments(@PathVariable("id") ObjectId vid,
                                   @RequestParam(defaultValue = "1") Integer page,
                                   @RequestParam(defaultValue = "10") Integer pagesize){
        PageResult pr = videosService.userCommentsList(vid,page,pagesize);
        return ResponseEntity.ok(pr);
    }

    /**
     * 发布视频评论
     */
    @PostMapping("/{id}/comments")
    public ResponseEntity upComments(@PathVariable("id") ObjectId id,@RequestBody Map map){
        String content = (String) map.get("comment");
        videosService.upComments(id,content);
        return ResponseEntity.ok(null);
    }
    /**
     * 评论点赞
     */
    @PostMapping(value = "/comments/{id}/like")
    public ResponseEntity likeComments(@PathVariable("id") String commentId){
        ObjectId objectId = new ObjectId(commentId);
        videosService.likeComments(objectId);
        return ResponseEntity.ok(null);
    }

    /**
     * 评论点赞取消
     */
    @PostMapping(value = "/comments/{id}/dislike")
    public ResponseEntity dislikeComments(@PathVariable("id") String commentId){
        ObjectId objectId = new ObjectId(commentId);
        videosService.dislikeComments(objectId);
        return ResponseEntity.ok(null);
    }
}